﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class xinxi_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            //检查用户是否登录
            int _user_id = DTRequest.GetFormInt("txtUserID", 0);
            BLL.xinxi bll = new BLL.xinxi();
            Model.xinxi model = bll.GetModel(id);
            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");

            //查看用户是否发信息本人
            bool is_owner = false;
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"type_id\":" + model.type_id);
            strTxt.Append(",\"user_id\":" + model.user_id);
            strTxt.Append(",\"title\":\"" + model.title + "\"");
            strTxt.Append(",\"price\":\"" + model.price + "/" + model.unit + "\"");
            if (model.type_id == 3) { strTxt.Append(",\"size\":\"\""); }
            else { strTxt.Append(",\"size\":\"" + model.size_long + "×" + model.size_wide + "×" + model.size_height + "mm\""); }
            
            string province_title = string.Empty;
            string city_title = string.Empty;
            if (model.province_id > 0)
            {
                province_title = new BLL.province().GetTitle(model.province_id);
            }
            if (model.city_id > 0)
            {
                city_title = new BLL.city().GetTitle(model.city_id);
            }
            strTxt.Append(",\"area\":\"" + province_title + city_title + "\"");
            strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\"");
            //strTxt.Append("\"remark\":\"" + Utils.ClearHtmlExceptA(model.content.ToString()) + "\",");
            strTxt.Append(",\"remark\":\"" + model.content.ToString() + "\"");

            if (model.user_id > 0)
            {
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel != null)
                {
                    strTxt.Append(",\"lianxiren\":\"" + userModel.user_name + "\"");
                    if (string.IsNullOrWhiteSpace(userModel.avatar))
                    {
                        strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                    }
                    else if (userModel.avatar.Contains("shicaiquanzi"))
                    {
                        //strTxt.Append(",\"avatar\":\"http://" + userModel.avatar + "\"");
                        if (userModel.avatar.Contains("http://"))
                        {
                            strTxt.Append(",\"avatar\":\"" + userModel.avatar + "\"");

                        }
                        else { strTxt.Append(",\"avatar\":\"http://" + userModel.avatar + "\""); }
                    }
                    else
                    {
                        strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + userModel.avatar + "\"");
                    }
                    if (_user_id > 0)
                    {
                        strTxt.Append(",\"mobile\":\"" + userModel.mobile + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"mobile\":\"登录后可见\"");
                    }
                    //添加判断 查看用户是否发信息本人
                    if (userModel.id == _user_id) { is_owner = true; }
                }
            }
            else
            {
                strTxt.Append(",\"lianxiren\":\"" + model.lianxiren.ToString() + "\"");
                strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                if (_user_id > 0)
                {
                    strTxt.Append(",\"mobile\":\"" + model.mobile.ToString() + "\"");
                }
                else
                {
                    strTxt.Append(",\"mobile\":\"登录后可见\"");
                }
                
            }

            string imglist = string.Empty;
            if (model.albums != null)
            {
                foreach (Model.xinxi_albums modelt in model.albums)
                {
                    if (!modelt.original_path.Contains("http://"))
                    {
                        imglist += "<div class=\"pic\"><img src=\"http://" + modelt.original_path + "\"/></div>";
                    }
                    else
                    {
                        imglist += "<div class=\"pic\"><img src=\"" + modelt.original_path + "\"/></div>";
                    }
                }
            }
            if (!string.IsNullOrEmpty(imglist))
            {
                strTxt.Append(",\"imglist\":\"" + Microsoft.JScript.GlobalObject.escape(imglist) + "\"");
            }
            else
            {
                strTxt.Append(",\"imglist\":\"\"");
            }
            strTxt.Append(",\"hits\":" + model.hits);
            //判断是否有传过来用户名、密码
            if (new BLL.collection().Exists(4, id, _user_id))
            {
                strTxt.Append(",\"is_collect\":1");
            }
            else
            {
                strTxt.Append(",\"is_collect\":0");
            }
            //评论内容
            strTxt.Append(",\"comment_list\":[");
            BLL.xinxi_comment bll2 = new BLL.xinxi_comment();
            DataSet ds = bll2.GetList(0, "status=0 and article_id=" + id, "id desc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"remark\":\"" + dr["remark"] + "\"");
                    strTxt.Append(",\"username\":\"" + new BLL.users().GetUserName(Convert.ToInt32(dr["user_id"])) + "\"");
                    strTxt.Append(",\"userid\":\"" + dr["user_id"].ToString() + "\"");
                    //strTxt.Append(",\"avatar\":\"http://" + new BLL.users().GetAvatar(Convert.ToInt32(dr["user_id"])) + "\"");
                    string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr["user_id"]));
                    if (_avatar.Contains("shicaiquanzi"))
                    {
                        if (_avatar.Contains("http://")) { strTxt.Append(",\"avatar\":\"" + _avatar + "\""); }
                        else { strTxt.Append(",\"avatar\":\"http://" + _avatar + "\""); }

                    }
                    else
                    {
                        strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + _avatar + "\"");
                    }

                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append("}");

                    //如果为本人 修改未读评论为已读
                    if (dr["is_read"].ToString() + "" == "0") {
                        if (is_owner) {
                            bll2.UpdateField(int.Parse(dr["id"] + ""), " is_read=1");
                        }
                    }


                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
            }
            strTxt.Append("]");
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}